
import { getApi, postApi } from '../request';
import {
  GetListRequestData, GetByIdRequestData,
  GetTitleByTypeRequestData, PullRequestData, EditRequestData,DelRequestData
} from './types'


//获取文章数目
export async function articleGetCounts() {
  try {
    const res:any = await getApi('article/articleCounts');
    return res;
  } catch (error) {
    console.error(error); // 处理错误
    return "error-0000"
  }
}

//获取文章列表
export async function articleGetList(data: GetListRequestData) {
  if (data.page_now * data.page_size < 0) { console.log("提交数据不符合要求！！！"); return "data-0000"; }

  try {
    const res:any = await postApi('article/getList', {
      "page_now": data.page_now,
      "page_size": data.page_size,
      "type": data.type,
      "tags": data.tags,
      "key_words": data.key_words
    });

    return res;
   

  } catch (error) {
    console.error(error); // 处理错误
    return "error-0000"
  }
}


//获取文章列表
export async function articleGetListByAuth() {
  try {
    const res:any = await getApi('article/getUserList');
  
    return res;
  } catch (error) {
    console.error(error); // 处理错误
    return "error-0000"
  }
}


//获取文章主体
export async function articleGetById(data: GetByIdRequestData) {
  if (data.article_id.length < 8) { console.log("提交数据不符合要求！！！"); return "data-0000"; }

  try {
    const res = await getApi('article/getById', {
      "article_id": data.article_id
    });
    return res;

  } catch (error) {
    console.error(error); // 处理错误
    return "error-0000"
  }
}

//获取文章的标题列表
export async function articleGetTitleByType(data: GetTitleByTypeRequestData) {
  if (data.tags.length == 0 || data.type.length == 0) { console.log("提交数据不符合要求！！！"); return "data-0000"; }
  try {
    const res = await postApi('article/getTitleByType', {
      "type": data.type,
      "tags": data.tags
    });
    return res;

  } catch (error) {
    console.error(error); // 处理错误
    return "error-0000"
  }
}

//发布文章
export async function articlePull(data: PullRequestData) {
  if (data.tags.length < 2 || data.type.length < 2 ||
    data.body.length < 50 || data.cover_image.length < 8
    || data.title.length < 4) { console.log("提交数据不符合要求！！！"); return "data-0000"; }
  try {
    const res = await postApi('article/pull', {
      "type": data.type,
      "tags": data.tags,
      "title": data.title,
      "body": data.body,
      "cover_image": data.cover_image
    });

    return res;

  } catch (error) {
    console.error(error); // 处理错误
    return "error-0000"
  }
}

//修改文章
export async function articleEdit(data: EditRequestData) {
  if (data.tags.length < 2 || data.type.length < 2 ||
    data.body.length < 50 
    || data.title.length < 4 
    || data.article_id.length < 8) { console.log("提交数据不符合要求！！！"); return "data-0000"; }

  try {
    const res = await postApi('article/edit', {
      "article_id": data.article_id,
      "type": data.type,
      "tags": data.tags,
      "title": data.title,
      "body": data.body,
      "cover_image": data.cover_image
    });
    return res;

  } catch (error) {
    console.error(error); // 处理错误
    return "error-0000"
  }
}

//删除
export async function articleDel(data:DelRequestData) {
  if (data.article_id.length<8) { console.log("提交数据不符合要求！！！"); return "data-0000"; }

  try {
    const res = await postApi('article/del',{
      "article_id":data.article_id
    });
    return res;
      
  } catch (error) {
    console.error(error); // 处理错误
    return "error-0000"
  }
}

//获取文章日志
export async function articleLog() {

  try {
    const res = await getApi('article/log');
    return res;

  } catch (error) {
    console.error(error); // 处理错误
    return "error-0000"
  }
}